package com.ydsungan.springtest.controller;

import com.alibaba.fastjson.JSONObject;
import com.ydsungan.springtest.annotation.UserLoginToken;
import com.ydsungan.springtest.config.UserConfig;
import com.ydsungan.springtest.pojo.User;
import com.ydsungan.springtest.service.TokenService;
import com.ydsungan.springtest.service.UserService;
import com.ydsungan.springtest.util.JSONResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(path = "/api")
public class HelloController {

    @Autowired
    private UserService userService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private UserConfig userConfig;

    private static final Logger logger = LoggerFactory.getLogger(HelloController.class);

    //登录
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Object login( @RequestBody User user){
        JSONObject jsonObject=new JSONObject();
        User userForBase=userService.findByUsername(user);
        if(userForBase==null){
            jsonObject.put("message","登录失败,用户不存在");
            return jsonObject;
        }else {
            if (!userForBase.getPassword().equals(user.getPassword())){
                jsonObject.put("message","登录失败,密码错误");
                return jsonObject;
            }else {
                String token = tokenService.getToken(userForBase);
                jsonObject.put("token", token);
                jsonObject.put("user", userForBase);
                return jsonObject;
            }
        }
    }

    @UserLoginToken
    @GetMapping("/getMessage")
    public JSONResult getMessage(){
        System.out.println("-------------------");

        logger.warn("warn");
        logger.error("error");
        logger.info("hahhhhaaaaa");
        return JSONResult.ok(userConfig.getOwnerDataPath());

    }
}
